package com.example.mytayunapi.Controller;

import com.example.mytayunapi.Entity.ProductImagesInfo;
import com.example.mytayunapi.Entity.productsInfo;
import com.example.mytayunapi.Service.ProductImagesInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("image")
public class ProductImagesController {

    @Value("${file.upload-dir.product-images}")
    private String uploadDir;

    @Autowired
    private ProductImagesInfoService productImagesInfoService;

    //http://localhost:8080/image/productimages
    @GetMapping("/productimages")
    public List<ProductImagesInfo> getProductImagesById(@RequestParam Integer productId) {
        return productImagesInfoService.getProductImagesById(productId);
    }

    @PostMapping("/add")
    public ResponseEntity<?> addProduct(@RequestParam("Product_name") String productName,
                                        @RequestParam("Product_price") String productPrice,
                                        @RequestParam("category") String category,
                                        @RequestParam("subcategory") String subcategory,
                                        @RequestParam("Product_detail") String productDetail,
                                        @RequestParam("Product_status") String productStatus,
                                        @RequestParam("School_degree") String schoolDegree,
                                        @RequestParam("Product_images") MultipartFile[] images) {

        // Save product info to Products table
        productsInfo product = new productsInfo();
        product.setProduct_name(productName);
        product.setProduct_price(productPrice);
        product.setCategory(category);
        product.setSubcategory(subcategory);
        product.setProduct_detail(productDetail);
        product.setProduct_status(productStatus);
        product.setSchool_degree(schoolDegree);

        // Simulate saving product and getting productId
        Integer productId = 1; // Replace with actual saved productId

        // Save each image to ProductImages table
        for (MultipartFile image : images) {
            try {
                String filename = UUID.randomUUID().toString() + "_" + image.getOriginalFilename();
                Path filePath = Paths.get(uploadDir, filename);
                Files.write(filePath, image.getBytes());

                ProductImagesInfo productImage = new ProductImagesInfo();
                productImage.setProduct_id(productId);
                productImage.setProduct_image(filePath.toString());

                productImagesInfoService.saveProductImagesInfo(productImage);

            } catch (IOException e) {
                return ResponseEntity.badRequest().body("文件上传失败: " + e.getMessage());
            }
        }

        Map<String, Object> response = new HashMap<>();
        response.put("message", "商品信息和图片上传成功");

        return ResponseEntity.ok(response);
    }
}
